package com.wen.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.common.utils.ResponseResult;
import com.wen.common.dto.UserDTO;
import com.wen.common.pojo.system.Permission;
import com.wen.common.pojo.system.User;
import com.wen.system.service.PermissionService;
import com.wen.system.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/system/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private PermissionService permissionService;

    @GetMapping(value = "/getMenusByUserId/{id}")
    @ApiOperation(value = "根据用户id获取用户菜单树")
    public ResponseResult<?> getMenuTree(@PathVariable("id") Long id) {
        List<Permission> menus = userService.getPermissionByUserId(id);
        // 构造出菜单树
        List<Permission> menuTree = permissionService.getMenuTree(menus, 0);
        return ResponseResult.success(menuTree);
    }



    @ApiOperation(value = "用户列表", notes = "模糊查询用户列表")
    @GetMapping("/findUserList")
    public ResponseResult<?> findUserList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                          UserDTO userdto) {
        Page<UserDTO> userList = userService.findUserPage(pageNum, pageSize, userdto);
        return ResponseResult.success(userList);
    }

    /**
     * 用户分页的查询
     */
    @ApiOperation(value = "用户列表", notes = "模糊查询用户列表")
    @GetMapping("/userPage")
    public ResponseResult<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "10") Integer pageSize,
                                      @RequestParam(defaultValue = "") String search) {

        Page<UserDTO> userPage = userService.findUserPage(pageNum, pageSize, search);
        return ResponseResult.success(userPage);
    }
    /**
     * 统计数据
     */
    @GetMapping("/count")
    public ResponseResult<?> count() {
        log.info("userService date: {}",userService.countByAddress());
        return ResponseResult.success(userService.countByAddress());
    }

    // 改变角色接口
    @PutMapping("/changeRole")
    public ResponseResult<?> changeRole(@RequestBody UserDTO user) {
        // 不重新登录
        return userService.changeUserRole(user);
    }

    // 新增用户
    @PostMapping("/add")
    public ResponseResult<?> save(@RequestBody User user) {
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }
        userService.insertUser(user);
        log.info("插入的用户信息：{}", user);
        return ResponseResult.success();
    }


    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @PostMapping("/delUser")
    public ResponseResult<?> delete(@RequestBody List<Long> ids) {
        // 批量删除
        userService.deleteBatch(ids);
        return ResponseResult.success();
    }

    @PostMapping("/modifyLockTime")
    public ResponseResult<?> modifyLockTime(@RequestParam int time) {
        return userService.modifyUserLockTime(time);
    }
    @PostMapping("/modifyLockNum")
    public ResponseResult<?> modifyLockNum(@RequestParam int num) {
        return userService.modifyUserLockNum(num);
    }

    // 修改
    @PutMapping("/update")
    public ResponseResult<?> update(@RequestBody User user) {
        userService.update(user);
        return ResponseResult.success();
    }

    @GetMapping("/{id}")
    public ResponseResult<?> getById(@PathVariable Long id) {
        return ResponseResult.success(userService.getUserById(id));
    }

    @GetMapping("/test")
    public void test() {
        System.out.println("test");
    }
}
